import { details, dictTree } from "./request.js";
import { formatURL, setBrowseNumber } from "./base.js";

$(function () {
	if ($(document.body).width() < 1000) {
		$(".content .collapse").removeClass("show");
	}
	$("#header").load("./layout/header.html", function () {
		$($(".navbarList > li")[3]).addClass("headerActive");
		$($(".navbarList > li")[3]).find("a").attr({
			href: "javascript:;"
		});
	});

	$("#footer").load("./layout/footer.html", function () {
		$("#accordionExample").on(
			"hide.bs.collapse show.bs.collapse",
			function (e) {
				let target = e.target;
				let partens = "#" + $("#" + target.id).attr("aria-labelledby");
				$(partens).find("i").toggleClass("bi-chevron-down");
				$(partens).find("i").toggleClass("bi-chevron-up");
			}
		);

        setBrowseNumber()
	});

    const getDictTree = () => {
        dictTree('INFORMATION_TYPE')
            .then((res) => {
                console.log(res);
                if (res.data.children.length > 0) {
                    let str = `<div class="tipText">新闻动态&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</div>`;
					let btnStr=res.data.children[0].label;
                    for (let index = 0; index < res.data.children.length; index++) {
                        const element = res.data.children[index];
                        if (element.label + '/' + element.dictCode == decodeURI(location.search.split("&")[1].split("=")[1])) {
                            str += `
                        <a href="./dynamic.html?label=${element.label}&type=${element.label + '/' + element.dictCode}" class="tabbar active" data-key="${element.label + '/' + element.dictCode}">

                        ${element.label}
                        </a>`;
                            btnStr = element.label;
						} else {
							str += `
                        <a href="./dynamic.html?label=${encodeURI(
													element.label
												)}&type=${encodeURI(
								element.label + "/" + element.dictCode
							)}" class="tabbar" data-key="${
								element.label + "/" + element.dictCode
							}">
                        ${element.label}
                        </a>`
                        }
                    }
                    $(".tabbar_m").html(str);
                    $('.showBox').html(' <div class="tipText">新闻动态&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</div>' + btnStr)

                }
            })
            .catch((err) => {
                console.log(err);
            });
        details(location.search.split("&")[0].split("=")[1]).then(res => {
            let mainBody = res.data.mainBody.replace(
							/http:\/\/wxmanage.hubeihome.net\/api/g,
							"https://hubeihome.net/api"
						);
            $(".text").html(mainBody);
            $(".container h3").html(res.data.title);
            $(".container .time").html(res.data.createTime);
            $(".share_wx img").attr({"src":formatURL(res.data.qrCode)})

        })
    };
    getDictTree();
    $(".share li img").click(function () {
        if ($(this).data("key") == "weibo") {
			var title = encodeURIComponent($(".container h3").text());
			var url = encodeURIComponent(window.location.href);
			let href='http://service.weibo.com/share/share.php?url=' + url + "&title=" + title;
			window.open(href);
		} else if ($(this).data("key") == "wx") {
            $(".shade").show()
            $(".share_wx").show()
        } else if ($(this).data("key") == "wxpyq") {

        } else if ($(this).data("key") == "url") {
            let url = location.href;//拿到想要复制的值
            let copyInput = document.createElement('input');//创建input元素
            copyInput.style.opacity = 0;
            document.body.appendChild(copyInput);//向页面底部追加输入框
            copyInput.setAttribute('value', url);//添加属性，将url赋值给input元素的value属性
            copyInput.select();//选择input元素
            document.execCommand("Copy");//执行复制命令
            $("#liveToast").show();
            setTimeout(() => {
                $("#liveToast").hide();
                copyInput.remove()
            }, 1500)
        }
    })
    $(".shade").click(function () {
        $(this).hide();
        $(".share_wx").hide();
    })
    // $(".share_wx").click(function () {
    //     $(".shade").hide();
    //     $(this).hide();
    // })
})
